// 
//  JPCaseManagementVC.m
//  JPKY
// 
//  Created by Vitas on 2016/11/13.
//  Copyright © 2016年 Vitas. All rights reserved.
// 

#import "JPCaseManagementVC.h"
#import "JPillCaseBottomView.h"
#import "ICSDrawerController.h"
#import "JPAddCaseController.h"
#import "JPillCaseCell.h"
#import "JPillnessModel.h"

static NSString *IllnessCaseCell = @"IllnessCaseCell";

@interface JPCaseManagementVC () <UITableViewDataSource>

/**
 *  病例tableView
 */
@property (strong, nonatomic) UITableView *caseTableView;

/**
 *  添加病例BottomView
 */
@property (strong, nonatomic) JPillCaseBottomView *addCaseView;

/**
 *  病例管理数组
 */
@property (nonatomic, strong) NSMutableArray *caseMangerArr;

/**
 *  要归解档的病例数组
 */
@property (strong, nonatomic) NSMutableArray *saveCaseArr;

@end

@implementation JPCaseManagementVC

@synthesize saveCaseArr = _saveCaseArr;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置navgationbar
    [self setNavgationBar];
    
    // 初始化UI
    [self setupUI];
    
    // 在Caches下创建一个illness文件夹存放病例
    [self createFolder];

    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
}

/**
 *  设置navgationbar
 */
- (void)setNavgationBar {
    UIButton *button = [[UIButton alloc] init];
    button.frame = CGRectMake(0, 0, 50, 40);
    [button setImage:[UIImage imageNamed:@"home_nav_button_back"] forState:UIControlStateNormal];
    button.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    [button addTarget:self action:@selector(didBackButton:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = leftItem;
}

/**
 *  初始化UI
 */
- (void)setupUI {
    [self.view addSubview:self.caseTableView];
    [self.view addSubview:self.addCaseView];
    
    // 自动布局子控件
    [self.caseTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.addCaseView.mas_top).offset(-20);
    }];
    
    [self.addCaseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(50);
        make.bottom.equalTo(self.view);
    }];
}

#pragma mark - UIUITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.saveCaseArr) {
        return self.saveCaseArr.count;
    }
    return 0;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JPillCaseCell *cell = [tableView dequeueReusableCellWithIdentifier:IllnessCaseCell];
    JPillCaseModel *model = self.saveCaseArr[indexPath.row];
    cell.caseModel = model;
    return cell;
}

#pragma mark - 响应事件

/**
 *  跳转到添加病例控制器
 */
- (void)didClickAddCaseView:(UIView*)view {
    JPAddCaseController *addCaseController = [[JPAddCaseController alloc] init];
    addCaseController.title = @"添加病例";
    
    [addCaseController setCallbackModel:^(JPillCaseModel * model) {
        // 获取病例模型
        [self.recordMangerArr addObject:model];
        self.saveCaseArr = self.recordMangerArr;
    }];
    [self.navigationController pushViewController:addCaseController animated:YES];
}

/**
 *  设置返回按钮
 */
- (void)didBackButton:(UIButton *)button {
    ICSDrawerController *drawer = (ICSDrawerController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    [drawer open];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Setter方法

/**
 *  设置病例管理数组
 */
- (void)setSaveCaseArr:(NSMutableArray *)saveCaseArr {
    _saveCaseArr = saveCaseArr;
    
    // 病例归档
    NSString *paths = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    NSString *casesPath = [paths stringByAppendingPathComponent:@"illness/Cases.data"];
    [NSKeyedArchiver archiveRootObject:_saveCaseArr toFile:casesPath];
    
    [self.caseTableView reloadData];
}


/**
 *  在Caches下面创建一个文件夹
 */
- (void)createFolder {
    NSString *paths = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    // 拼接路径
    NSString *illnessPath = [paths stringByAppendingPathComponent:@"illness"];
    // 创建文件夹
    NSFileManager *manager = [NSFileManager defaultManager];
    [manager createDirectoryAtPath:illnessPath withIntermediateDirectories:YES attributes:nil error:nil];
}

#pragma mark - 懒加载子控件

- (UITableView *)caseTableView {
    if (!_caseTableView) {
        _caseTableView = [[UITableView alloc] init];
        
        // 注册cell
        [_caseTableView registerNib:[UINib nibWithNibName:@"JPillCaseCell" bundle:nil] forCellReuseIdentifier:IllnessCaseCell];
        // 设置数据源
        _caseTableView.dataSource = self;
        _caseTableView.rowHeight = 150;
        _caseTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _caseTableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    return _caseTableView;
}

- (JPillCaseBottomView *)addCaseView {
    if (!_addCaseView) {
        // 创建点击手势
        UITapGestureRecognizer*tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickAddCaseView:)];
        
        JPillCaseBottomView *bottomView = [[JPillCaseBottomView alloc] init];
        bottomView.backgroundColor = [UIColor whiteColor];
        [bottomView addGestureRecognizer:tapGesture];
        _addCaseView = bottomView;
    }
    return _addCaseView;
}

/**
 *  储存病例数组
 */
- (NSMutableArray *)recordMangerArr {
    if (!_caseMangerArr) {
        // 从本地解档
        NSString *paths = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
        NSString *casesPath = [paths stringByAppendingPathComponent:@"illness/Cases.data"];
        _caseMangerArr = [NSKeyedUnarchiver unarchiveObjectWithFile:casesPath];
        
        if (!_caseMangerArr) {
            _caseMangerArr = [NSMutableArray array];
        }
    }
    return _caseMangerArr;
}

- (NSMutableArray *)saveCaseArr {
    if (!_saveCaseArr) {
        // 本地解档
        NSString *paths = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
        NSString *casesPath = [paths stringByAppendingPathComponent:@"illness/Cases.data"];
        _saveCaseArr = [NSKeyedUnarchiver unarchiveObjectWithFile:casesPath];
        if (!_saveCaseArr) {
            _saveCaseArr = [NSMutableArray array];
        }
    }
    return _saveCaseArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
